const express = require('express');
const bodyParser = require('body-parser')
const cors = require('cors')
const mysql = require('mysql');
const app  = express();

app.use(bodyParser.json());
app.use(cors({ origin: "*"
}));
app.all('*', function (req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Credentials', true);
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
})
const connection = mysql.createConnection({
    host : 'localhost',
    user:'root',
    password : 'root',
    database : 'express'
});
connection.connect((err) => {
    if (err)
        console.log('Error connection to MYSQL database:' + err)
    return;
});
connection.query('select * from formData',(err,results,field) => {
    if (err) throw err;
    app.post('/api', (req, res) => {
        res.send(results)
    })
    app.listen(3000, () => {
        console.log('Server statred on port 3000')
    })
});
// connection.end((err) => {
//     if (err) {
//         console.log('Error connection:' + err.stack);
//         return;
//     }
//     console.log('MySQL database connection closed')
// });
